package com.study.mythread.model;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @program: java-study-of-learner
 * @description: callable方式实现线程，有返回值
 * @author: future
 * @create: 2021-01-16 19:09
 **/
public class MyCallable implements Callable<String> {
    //callable y业务结构有返回值
    @Override
    public String call() throws Exception {
        int num =1;
        while(num!=20){
            System.out.println(Thread.currentThread().getName()+" callable输出 "+ num++);
        }

        return "10";
    }

    public static void main(String[] args) {
        MyCallable my = new MyCallable();
        FutureTask<String> task = new FutureTask<>(my);
        Thread th = new Thread(task);
        th.start();
        try {
            System.out.println(task.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
